//
//  Utente.m
//  InterventixApp
//
//  Created by Diego Falcinelli on 08/05/13.
//
//

#import "Utente.h"
#import "Define.h"
#import "CryptoCR2.h"
#import "SBJson.h"
#import "Syncronize.h"

@implementation Utente

-(NSMutableDictionary *) convertToDictionary{
    NSMutableDictionary *diz = [NSMutableDictionary dictionaryWithCapacity:0];
    [diz setObject:self.idutente        forKey:@"idutente"];
    [diz setObject:self.nome            forKey:@"nome"];
    if(self.cognome != nil){
        [diz setObject:self.cognome         forKey:@"cognome"];
    }else{
        [diz setObject:[NSNull null]        forKey:@"cognome"];
    }
    [diz setObject:self.username        forKey:@"username"];
    [diz setObject:self.password        forKey:@"password"];
    [diz setObject:self.email           forKey:@"email"];
    [diz setObject:self.tipo            forKey:@"tipo"];
    [diz setObject:self.revisione       forKey:@"revisione"];
    if(self.cancellato){
        [diz setObject:@true            forKey:@"cancellato"];
    }else{
        [diz setObject:@false           forKey:@"cancellato"];
    }
    
    if(self.cestinato){
        [diz setObject:@true            forKey:@"cestinato"];
    }else{
        [diz setObject:@false           forKey:@"cestinato"];
    }
    return diz;
}

-(void) readFromMutableDictionary:(NSMutableDictionary *) diz{
    self.idutente       = [NSNumber numberWithInt:[[diz objectForKey:@"idutente"] intValue]];
    self.nome           = [diz objectForKey:@"nome"];
    self.cognome        = [diz objectForKey:@"cognome"];
    self.username       = [diz objectForKey:@"username"];
    self.password       = [diz objectForKey:@"password"];
    self.email          = [diz objectForKey:@"email"];
    self.tipo           = [diz objectForKey:@"tipo"];
    self.revisione      = [NSNumber numberWithInt:[[diz objectForKey:@"revisione"] intValue]];
    self.cancellato     = [[diz objectForKey:@"cancellato"] boolValue];
    self.cestinato      = [[diz objectForKey:@"cestinato"] boolValue];
}

-(void) readFromDictionary:(NSDictionary *) diz{
    self.idutente       = [NSNumber numberWithInt:[[diz objectForKey:@"idutente"] intValue]];
    self.nome           = [diz objectForKey:@"nome"];
    self.cognome        = [diz objectForKey:@"cognome"];
    self.username       = [diz objectForKey:@"username"];
    self.password       = [diz objectForKey:@"password"];
    self.email          = [diz objectForKey:@"email"];
    self.tipo           = [diz objectForKey:@"tipo"];
    self.revisione      = [NSNumber numberWithInt:[[diz objectForKey:@"revisione"] intValue]];
    self.cancellato     = [[diz objectForKey:@"cancellato"] boolValue];
    self.cestinato      = [[diz objectForKey:@"cestinato"] boolValue];
}

-(void) load{
    //Utente *ulog =[Syncronize getUserLog];
    //NSArray *all = [Syncronize getusers:ulog];
    /*for(Utente *us in all){
        if([us.idutente longLongValue] == [self.idutente longLongValue]){
            self.nome = us.nome;
            self.cognome = us.cognome;
            self.username = us.cognome;
            self.password = us.password;
            self.email = us.email;
            self.tipo = us.tipo;
            self.revisione = us.revisione;
            self.cancellato = us.cancellato;
            self.cestinato = us.cestinato;
        }
  }*/
}


+(Utente *) createFromDictionary:(NSDictionary *)diz{
    Utente *u = [[Utente alloc]init];
    [u readFromDictionary:diz];
    return u;
}


-(unsigned long long) hashcode{
    unsigned long long h = [self.idutente hash];
    h+= [self.nome hash];
    h+= [self.cognome hash];
    h+= [self.username hash];
    h+= [self.password hash];
    h+= [self.email hash];
    h+= [self.tipo hash];
    h+= [self.revisione hash] * 331;
    h+= self.cancellato + self.cestinato;
    return h;
}

@end
